package com.example.config.mybatisPlus;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.example.util.DateUtils;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.apache.ibatis.binding.MapperMethod;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

@Configuration
public class MybatisPlusSaasConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //分表
        MyDynamicTableNameInterceptor myDynamicTableNameInterceptor = new MyDynamicTableNameInterceptor();

        //这里的写法:使用函数式接口，采用lambda表达式
        HashMap<String, MyTableNameHandler> map = new HashMap<String, MyTableNameHandler>(2) {{
            //整个函数返回的结果就是替换后的新表名，这个生成的表名的规则可以自己随便指定
            // 获取传入参数 year，如果有的话做为后缀，没有的话则使用当前年份作为后缀
            //有效工日表
            put("product", (sql, tableName, para) -> {
                String year = getYear(para);
                return tableName + (StringUtils.isEmpty(year) ? "" : "_") + year;
            });
            //工日打卡流程表
            put("jh_workdate_clock_flow", (sql, tableName, para) -> {
                String year = getYear(para);
                return tableName + (StringUtils.isEmpty(year) ? "" : "_") + year;
            });
            //工日核算号表
            put("jh_workdate_hsh", (sql, tableName, para) -> {
                String year = getYear(para);
                return tableName + (StringUtils.isEmpty(year) ? "" : "_") + year;
            });
            //核算号统计表
            put("jh_workdate_hsh_statistics", (sql, tableName, para) -> {
                String year = getYear(para);
                return tableName + (StringUtils.isEmpty(year) ? "" : "_") + year;
            });
        }};

        myDynamicTableNameInterceptor.setTableNameHandlerMap(map);
        interceptor.addInnerInterceptor(myDynamicTableNameInterceptor);

        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());

        return interceptor;
    }

    /**
     * 获取年份
     *
     * @param para
     * @return
     */
    public String getYear(Object para) {
        String year = "";
        if (para instanceof MapperMethod.ParamMap) {
            MapperMethod.ParamMap param = (MapperMethod.ParamMap) para;
            year = param != null && param.containsKey("year") && param.get("year") != null ? String.valueOf(param.get("year")) : "";
            //注意这里的去value的key要和 mapper方法的入参名称一致。比如这里是year，mapper方法的入参也要是year
        }

        //如果没有传年份参数，默认使用当前年份
        if (StringUtils.isEmpty(year) || year.length() != 4) {
            year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
        }

        if (Convert.toInt(year) > DateUtils.getYear() || Convert.toInt(year) < 2021) {
            return "";
        }
        return year;
    }



}
